<template>
    <h1>已经填写过的问卷</h1>
    <div class="survey-table-container">
      <el-card>
        <el-table :data="surveys" stripe style="width: 100%">
            <el-table-column label="" width="150" align="center">
          <template #default="scope">
            <el-button @click="viewDetails(scope.row.survey.id,this.$router)">查看详情</el-button>
          </template>
        </el-table-column>
          <el-table-column prop="survey.title" label="问卷标题" width="400" align="center"></el-table-column>
          <el-table-column prop="survey.id" label="问卷id" width="120" align="center"></el-table-column>
          <el-table-column prop="survey.status" label="问卷状态" width="150" align="center"></el-table-column>
          <el-table-column prop="survey.username" label="发布者" width="250" align="center"></el-table-column>
          <el-table-column prop="time" label="填写时间" width="200" align="center"></el-table-column>
        </el-table>
      </el-card>
    </div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue';
  import request from '../utils/request';
  import router from '../router/router';
  import { ElMessage } from 'element-plus';
  
  export default {
    name: 'SurveyTable',
    setup() {
      const surveys = ref([]);
  
      const fetchSurveys = async () => {
        try {
          const response = await request.get('api/commit/committedSurvey');
          //console.log('API response:', response); 
          if (response.code === "200") {
            surveys.value = response.data.commitList;
            console.log('Surveys data:', surveys.value);
          } else {
            ElMessage.error(response.data.msg);
          }
        } catch (error) {
          console.error('Error fetching surveys:', error);
        }
      };
  
      const viewDetails= async (id,router) =>{
        localStorage.setItem('s_id',id);
        router.push('/FilledSurveys');
        console.log("1");
      };
      
      onMounted(() => {
        fetchSurveys();
      });
  
      return { surveys,
        viewDetails,
       };
    }
  };
  </script>
  
  <style scoped>
  .survey-table-container {
    padding: 20px;
  }
  
  .el-table th, .el-table td {
    text-align: center;
  }
  
  .el-card {
    padding: 20px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .el-table .cell {
    padding: 10px;
  }
  </style>
  